<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Código-Fonte de genetic_operator.h</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javaScript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Gerado por Doxygen 1.6.3 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Busca');
--></script>
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;Principal</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Arquivos</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <img id="MSearchSelect" src="search/search.png"
             onmouseover="return searchBox.OnSearchSelectShow()"
             onmouseout="return searchBox.OnSearchSelectHide()"
             alt=""/>
        <input type="text" id="MSearchField" value="Busca" accesskey="S"
             onfocus="searchBox.OnSearchFieldFocus(true)" 
             onblur="searchBox.OnSearchFieldFocus(false)" 
             onkeyup="searchBox.OnSearchFieldChange(event)"/>
        <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
        </div>
      </li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>Lista&nbsp;de&nbsp;Arquivos</span></a></li>
      <li><a href="globals.html"><span>Arquivos&nbsp;Membros</span></a></li>
    </ul>
  </div>
<h1>genetic_operator.h</h1><a href="genetic__operator_8h.html">Vá para a documentação deste arquivo.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00008"></a>00008 <span class="preprocessor">#ifndef GENETIC_OPERATOR_H</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span><span class="preprocessor">#define GENETIC_OPERATOR_H</span>
<a name="l00010"></a>00010 <span class="preprocessor"></span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;string&gt;</span>
<a name="l00012"></a>00012 
<a name="l00013"></a>00013 <span class="preprocessor">#include &quot;<a class="code" href="population_8h.html">population.h</a>&quot;</span>
<a name="l00014"></a>00014 <span class="preprocessor">#include &quot;<a class="code" href="definitions_8h.html">definitions.h</a>&quot;</span>
<a name="l00015"></a>00015 
<a name="l00016"></a>00016 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00023"></a>00023 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00024"></a>00024 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _ty = GAtype,<span class="keyword">typename</span> _realTy = GAreal_type&gt; <span class="keyword">class </span><a class="code" href="classgenetic__operator.html">genetic_operator</a>;
<a name="l00025"></a>00025 
<a name="l00026"></a>00026 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00027"></a><a class="code" href="classgenetic__operator.html">00027</a> <span class="keyword">class </span><a class="code" href="classgenetic__operator.html">genetic_operator</a>
<a name="l00028"></a>00028 {
<a name="l00029"></a>00029   <span class="keyword">public</span>:
<a name="l00030"></a>00030 
<a name="l00031"></a>00031     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00038"></a>00038     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00039"></a><a class="code" href="classgenetic__operator.html#a114de57cea1aa2b6db81389ddf5be520">00039</a>     <a class="code" href="classgenetic__operator.html#a114de57cea1aa2b6db81389ddf5be520">genetic_operator</a> (<a class="code" href="classpopulation.html">population&lt;_ty,_realTy&gt;</a>* pt_to_apply_operator = NULL,<span class="keyword">const</span> <span class="keywordtype">float</span>&amp; probability = def::genetic_operator::probability)
<a name="l00040"></a>00040                      :_to_apply_operator(pt_to_apply_operator),_probability(probability){};
<a name="l00041"></a>00041     
<a name="l00042"></a>00042     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00046"></a>00046     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00047"></a>00047     <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classgenetic__operator.html#a70569d059114efa73bcd45a264953d13">doApplyGeneticOperator</a> (<span class="keywordtype">void</span>);
<a name="l00048"></a>00048 
<a name="l00049"></a>00049     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00059"></a>00059     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00060"></a>00060     <span class="keyword">const</span> _realTy <a class="code" href="classgenetic__operator.html#a406eeb5144dd74698fcd9a120f021aba">GenerateRandom</a> (<span class="keyword">const</span> _realTy&amp; max = 1,<span class="keyword">const</span> _realTy&amp; min = 0, <span class="keyword">const</span> <span class="keywordtype">int</span>&amp; precision = def::genetic_operator::precision);
<a name="l00061"></a>00061 
<a name="l00062"></a>00062     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00071"></a>00071     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00072"></a>00072     <span class="keyword">const</span> _ty <a class="code" href="classgenetic__operator.html#a406eeb5144dd74698fcd9a120f021aba">GenerateRandom</a> (<span class="keyword">const</span> _ty&amp; max,<span class="keyword">const</span> _ty&amp; min = 0);
<a name="l00073"></a>00073 
<a name="l00074"></a>00074     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00082"></a>00082     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00083"></a>00083     <span class="keyword">virtual</span> <span class="keyword">const</span> <span class="keywordtype">char</span>&amp; <a class="code" href="classgenetic__operator.html#a3405bb5335111bd675d408aa8db052fa">WalkOnPopulationHook</a> (<a class="code" href="classindividual.html">individual&lt;_ty,_realTy&gt;</a>&amp; <span class="keywordtype">id</span>) =0;
<a name="l00084"></a>00084     
<a name="l00085"></a>00085     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00093"></a>00093     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00094"></a>00094     <span class="keyword">virtual</span> <span class="keyword">const</span> <span class="keywordtype">char</span>&amp; <a class="code" href="classgenetic__operator.html#a2124d70b28b35d3114eb3e3ffa72baef">WalkOnIndividualHook</a> (<a class="code" href="classcoordinate.html">coordinate&lt;_ty,_realTy&gt;</a>&amp; coo) =0;
<a name="l00095"></a>00095 
<a name="l00096"></a>00096     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00102"></a>00102     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00103"></a><a class="code" href="classgenetic__operator.html#ae0f79368c0b4ad0cff3f608727bd87f5">00103</a>     <span class="keyword">virtual</span> std::string <a class="code" href="classgenetic__operator.html#ae0f79368c0b4ad0cff3f608727bd87f5">GetName</a> (<span class="keywordtype">void</span>) {<span class="keywordflow">return</span> std::string(<span class="stringliteral">&quot;genetic_operator&quot;</span>);}
<a name="l00104"></a>00104 
<a name="l00105"></a>00105   <span class="keyword">protected</span>:
<a name="l00106"></a>00106 
<a name="l00107"></a>00107     <a class="code" href="classpopulation.html">population&lt;_ty,_realTy&gt;</a>* _to_apply_operator;<span class="comment">//ponteiro para a coordenada que sofrerá a mutação</span>
<a name="l00108"></a>00108     <span class="keywordtype">float</span> _probability;<span class="comment">//probabilidade de mutaçao, a classe base definirá se é bit a bit, de coordenada ou indivíduo</span>
<a name="l00109"></a>00109 
<a name="l00110"></a>00110 };
<a name="l00111"></a>00111 
<a name="l00112"></a>00112 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00113"></a><a class="code" href="classgenetic__operator.html#a70569d059114efa73bcd45a264953d13">00113</a> <span class="keywordtype">void</span> <a class="code" href="classgenetic__operator.html#a70569d059114efa73bcd45a264953d13">genetic_operator&lt;_ty,_realTy&gt;::doApplyGeneticOperator</a> (<span class="keywordtype">void</span>)
<a name="l00114"></a>00114 {
<a name="l00115"></a>00115   <span class="keywordflow">for</span> (<span class="keyword">typename</span> <a class="code" href="classpopulation.html">population&lt;_ty,_realTy&gt;::const_it_</a> it = _to_apply_operator-&gt;begin(); it != _to_apply_operator-&gt;end(); ++it)
<a name="l00116"></a>00116   {
<a name="l00117"></a>00117     <span class="keywordtype">char</span> direction = <a class="code" href="classgenetic__operator.html#a3405bb5335111bd675d408aa8db052fa">WalkOnPopulationHook</a>(*(*it));<span class="comment">//aplica o operador nos indivíduos da população</span>
<a name="l00118"></a>00118     <span class="keywordflow">if</span> (direction == def::genetic_operator::go_up)
<a name="l00119"></a>00119       <span class="keywordflow">return</span>;<span class="comment">//termina com o operador</span>
<a name="l00120"></a>00120     <span class="keywordflow">else</span> <span class="keywordflow">if</span> (direction == def::genetic_operator::go_down)
<a name="l00121"></a>00121     {<span class="comment">//Se o operador é aplicado nas coordenadas desce para o vetor de coordenadas do indivíduo</span>
<a name="l00122"></a>00122       <span class="keywordflow">for</span> (<span class="keyword">typename</span> <a class="code" href="classindividual.html">individual&lt;_ty,_realTy&gt;::const_it_</a> it_id = (*it)-&gt;begin(); it_id != (*it)-&gt;end(); ++it_id)
<a name="l00123"></a>00123       {
<a name="l00124"></a>00124         <span class="keywordtype">char</span> other_direction = <a class="code" href="classgenetic__operator.html#a2124d70b28b35d3114eb3e3ffa72baef">WalkOnIndividualHook</a>(*(*it_id));<span class="comment">//aplica o operador nas coordenadas do individuo (*it_id) </span>
<a name="l00125"></a>00125         <span class="keywordflow">if</span> (other_direction == def::genetic_operator::go_up)
<a name="l00126"></a>00126           <span class="keywordflow">break</span>;
<a name="l00127"></a>00127       }
<a name="l00128"></a>00128     }
<a name="l00129"></a>00129   }
<a name="l00130"></a>00130 }
<a name="l00131"></a>00131 
<a name="l00132"></a>00132 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00133"></a><a class="code" href="classgenetic__operator.html#a406eeb5144dd74698fcd9a120f021aba">00133</a> <span class="keyword">const</span> _realTy <a class="code" href="classgenetic__operator.html#a406eeb5144dd74698fcd9a120f021aba">genetic_operator&lt;_ty,_realTy&gt;::GenerateRandom</a> (<span class="keyword">const</span> _realTy&amp; max, <span class="keyword">const</span> _realTy&amp; min,<span class="keyword">const</span> <span class="keywordtype">int</span>&amp; precision)
<a name="l00134"></a>00134 {
<a name="l00135"></a>00135   _realTy to_int_max = max*pow(10,precision);
<a name="l00136"></a>00136   _realTy to_int_min = min*pow(10,precision);
<a name="l00137"></a>00137   <span class="keywordtype">int</span> int_min = floor(to_int_min);
<a name="l00138"></a>00138   <span class="keywordtype">int</span> int_max = floor(to_int_max);
<a name="l00139"></a>00139   <span class="keywordtype">int</span> random = int_min + rand()%int_max +1;
<a name="l00140"></a>00140   _realTy ret = _realTy(random);
<a name="l00141"></a>00141   ret *= pow(10,-precision);
<a name="l00142"></a>00142   <span class="keywordflow">return</span> ret;
<a name="l00143"></a>00143 }
<a name="l00144"></a>00144 
<a name="l00145"></a>00145 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00146"></a><a class="code" href="classgenetic__operator.html#ad9f7ea3ff91063cde8959794b1ac5679">00146</a> <span class="keyword">const</span> _ty <a class="code" href="classgenetic__operator.html#a406eeb5144dd74698fcd9a120f021aba">genetic_operator&lt;_ty,_realTy&gt;::GenerateRandom</a> (<span class="keyword">const</span> _ty&amp; max, <span class="keyword">const</span> _ty&amp; min)
<a name="l00147"></a>00147 {
<a name="l00148"></a>00148   _ty random = min + _ty(rand())%max + 1;
<a name="l00149"></a>00149   <span class="keywordflow">return</span> random;
<a name="l00150"></a>00150 }
<a name="l00151"></a>00151 
<a name="l00152"></a>00152 <span class="preprocessor">#endif //GENETIC_OPERATOR_H</span>
</pre></div></div>
<!--- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&nbsp;</span>Todos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&nbsp;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&nbsp;</span>Arquivos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&nbsp;</span>Funções</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&nbsp;</span>Variáveis</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&nbsp;</span>Definições de Tipos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&nbsp;</span>Amigas</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<hr class="footer"/><address style="text-align: right;"><small>Gerado em Sat Oct 30 13:47:50 2010 por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address>
</body>
</html>
